package space.misiro.ledgers.middleware.api.domain.um

import com.fasterxml.jackson.annotation.JsonIgnore
import io.swagger.v3.oas.annotations.media.Schema
import java.time.LocalDate

/**
 * 代表AIS（账户信息服务）同意请求的数据传输对象
 *
 * @param id 同意的唯一标识符
 * @param userId 对应支付服务用户（PSU）的唯一标识
 * @param tppId 第三方服务提供商（TPP）的唯一标识
 * @param frequencyPerDay 每日最大访问频率，单次访问应设置为1
 * @param access 用户授予的账户访问权限集合
 * @param validUntil 同意的有效期截止日期（ASPSP本地日期，ISO格式）
 * @param recurringIndicator 标识是否允许重复访问账户数据（true表示允许定期访问）
 */
@Schema(description = "Ais consent request", name = "AisConsentRequest")
data class AisConsentTO(

    @field:Schema(description = "The consent id", required = true)
    val id: String,

    @field:Schema(description = "Corresponding PSU", required = true)
    val userId: String,

    @field:Schema(description = "ID of the corresponding TPP.", required = true, example = "testTPP")
    val tppId: String,

    @field:Schema(
        description = "Maximum frequency for an access per day. For a once-off access, this attribute is set to 1",
        required = true, example = "4"
    )
    val frequencyPerDay: Int,

    @field:Schema(description = "Set of access given by psu for this account", required = true)
    val access: AisAccountAccessInfoTO,

    @field:Schema(
        description = "Consent's expiration date. The content is the local ASPSP date in ISODate Format",
        required = true,
        example = "2020-10-10"
    )
    val validUntil: LocalDate,

    @field:Schema(
        description = "'true', if the consent is for recurring access to the account data, 'false', if the consent is for one access to the account data",
        required = true,
        example = "false"
    )
    val recurringIndicator: Boolean
) {
    /**
     * 验证当前同意是否有效（未过期）
     *
     * @return 当有效期截止日大于当前日期时返回true，否则返回false
     */
    @get:JsonIgnore
    val validConsent: Boolean
        get() = validUntil.isAfter(LocalDate.now())

    /**
     * 清理PIIS（支付账户信息）相关数据
     *
     * 将账户访问权限重置为默认值，并清空账户列表和交易列表。
     * 用于处理需要保护支付账户敏感信息的场景。
     */
    @JsonIgnore
    fun cleanupForPIIS() {
        access.allPsd2 = AisAccountAccessTypeTO.DEFAULT
        access.availableAccounts = AisAccountAccessTypeTO.DEFAULT
        access.accounts = emptyList()
        access.transactions = emptyList()
    }
}
